/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.examples.rcp.texteditor.actions;

import org.eclipse.jface.action.ICoolBarManager;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;

/**
 * An action bar advisor is responsible for creating, adding, and disposing of
 * the actions added to a workbench window. Each window will be populated with
 * new actions.
 */
public class TextEditorActionBarAdvisor extends ActionBarAdvisor {

	// Actions - important to allocate these only in makeActions, and then use
	// them
	// in the fill methods. This ensures that the actions aren't recreated
	// when fillActionBars is called with FILL_PROXY.
	private static IWorkbenchAction newAction;
	private static IWorkbenchAction exitAction;
	private static IWorkbenchAction aboutAction;
	private static IWorkbenchAction copyAction;
	private static IWorkbenchAction pasteAction;
	private static IWorkbenchAction cutAction;
	private static IWorkbenchAction undoAction;
	private static IWorkbenchAction redoAction;
	private static IWorkbenchAction saveAsAction;
	private static IWorkbenchAction saveAction;
	private static IWorkbenchAction selectAll;
	private static IWorkbenchAction find;
	private static IWorkbenchAction closeAction;
	private static IWorkbenchAction closeAllAction;
	private static IWorkbenchAction preferences;
	private static IWorkbenchAction help;
	private static IWorkbenchAction intro;
	private static OpenFileAction open;

	public TextEditorActionBarAdvisor(IActionBarConfigurer configurer) {
		super(configurer);
	}

	protected void makeActions(final IWorkbenchWindow window) {
		// Creates the actions and registers them.
		// Registering is needed to ensure that key bindings work.
		// The corresponding commands keybindings are defined in the plugin.xml
		// file.
		// Registering also provides automatic disposal of the actions when
		// the window is closed.

		newAction = ActionFactory.NEW.create(window);
		newAction.setText("New..");
		register(newAction);

		exitAction = ActionFactory.QUIT.create(window);
		register(exitAction);

		aboutAction = ActionFactory.ABOUT.create(window);
		register(aboutAction);

		copyAction = ActionFactory.COPY.create(window);
		register(copyAction);

		cutAction = ActionFactory.CUT.create(window);
		register(cutAction);

		pasteAction = ActionFactory.PASTE.create(window);
		register(pasteAction);

		undoAction = ActionFactory.UNDO.create(window);
		register(undoAction);

		redoAction = ActionFactory.REDO.create(window);
		register(redoAction);

		saveAction = ActionFactory.SAVE.create(window);
		register(saveAction);

		saveAsAction = ActionFactory.SAVE_AS.create(window);
		register(saveAsAction);

		selectAll = ActionFactory.SELECT_ALL.create(window);
		register(selectAll);

		find = ActionFactory.FIND.create(window);
		register(find);

		closeAction = ActionFactory.CLOSE.create(window);
		register(closeAction);

		closeAllAction = ActionFactory.CLOSE_ALL.create(window);
		register(closeAllAction);

		preferences = ActionFactory.PREFERENCES.create(window);
		register(preferences);

		open = new OpenFileAction(window);
		register(open);

		help = ActionFactory.HELP_CONTENTS.create(window);
		register(help);

		intro = ActionFactory.INTRO.create(window);
		register(intro);
	}

	protected void fillMenuBar(IMenuManager menuBar) {
		// File menu
		MenuManager fileMenu = new MenuManager("&File",
				IWorkbenchActionConstants.M_FILE);
		fileMenu.add(open);
		fileMenu.add(newAction);
		fileMenu.add(new Separator("save"));
		fileMenu.add(saveAction);
		fileMenu.add(saveAsAction);
		fileMenu.add(new Separator("closers"));
		fileMenu.add(closeAction);
		fileMenu.add(closeAllAction);
		fileMenu.add(new Separator("exiters"));
		fileMenu.add(exitAction);

		// Edit menu
		MenuManager editMenu = new MenuManager("&Edit",
				IWorkbenchActionConstants.M_EDIT);
		editMenu.add(new Separator("copy"));
		editMenu.add(copyAction);
		editMenu.add(cutAction);
		editMenu.add(pasteAction);
		editMenu.add(new Separator("select"));
		editMenu.add(selectAll);
		editMenu.add(find);
		editMenu.add(new Separator("undos"));
		editMenu.add(undoAction);
		editMenu.add(redoAction);

		// Window menu
		MenuManager windowMenu = new MenuManager("&Window",
				IWorkbenchActionConstants.M_WINDOW);
		windowMenu.add(preferences);

		// Help menu
		MenuManager helpMenu = new MenuManager("&Help",
				IWorkbenchActionConstants.M_HELP);
		helpMenu.add(aboutAction);
		helpMenu.add(help);
		helpMenu.add(intro);
		menuBar.add(fileMenu);
		menuBar.add(editMenu);
		menuBar.add(windowMenu);
		menuBar.add(helpMenu);
	}

	protected void fillCoolBar(ICoolBarManager coolBar) {
		IToolBarManager projCoolBar = new ToolBarManager(coolBar.getStyle());
		coolBar.add(projCoolBar);
		IToolBarManager editCoolBar = new ToolBarManager(coolBar.getStyle());
		coolBar.add(editCoolBar);
		editCoolBar.add(copyAction);
		editCoolBar.add(cutAction);
		editCoolBar.add(pasteAction);
		editCoolBar.add(undoAction);
		editCoolBar.add(redoAction);
	}
}
